package com.sunfeng.n4.crossoutput;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author sunfeng
 * @date 2021/03/31 17:57
 **/
public class SyncParkUnparkTest {
     static Thread t1;
     static Thread t2;
     static Thread t3;

    public static void main(String[] args) {
        SyncParkUnpark syncParkUnpark = new SyncParkUnpark(5);
        t1 = new Thread(() -> {
            syncParkUnpark.print("a", t2);
        });
        t1.start();

        t2 = new Thread(() -> {
            syncParkUnpark.print("b", t3);
        });
        t2.start();

        t3 = new Thread(() -> {
            syncParkUnpark.print("c", t1);
        });
        t3.start();


        LockSupport.unpark(t1);

    }

}


class SyncParkUnpark {

    public void print(String str, Thread next) {

        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }

    }

    private int loopNumber;

    public SyncParkUnpark(int loopNumber) {
        this.loopNumber = loopNumber;
    }
}